#ifndef _AUDIO_SVR_H
#define _AUDIO_SVR_H

#include "base.h"
#include "audio_codec.h"

#define AUDIO_RENDER_CHANNEL 0

class CAudio_svr
{
public:
	CAudio_svr(LPST_ENV_T pEnv);
	virtual ~CAudio_svr();
private:
	void InitEnc();
	void ReleaseEnc();
	void InitDec();
	void ReleaseDec();
	static void* cbCapAudioThreadFunc(void* pParam){((CAudio_svr*)pParam)->CapAudioThreadFunc(); return NULL;};
	void CapAudioThreadFunc();
	static void* cbEncThreadFunc(void* pParam){((CAudio_svr*)pParam)->EncThreadFunc(); return NULL;};
	void EncThreadFunc();
	static void* cbPlayThreadFunc(void* pParam){((CAudio_svr*)pParam)->PlayThreadFunc(); return NULL;};
	void PlayThreadFunc();
	static void* cbDecThreadFunc(void* pParam){((CAudio_svr*)pParam)->DecThreadFunc(); return NULL;};
	void DecThreadFunc();

//	void PutFrameToEncMem(int nChId, gm_enc_bitstream_t *pBs);

	void PutFrameToEmptyMem(LPST_AUDIO_FRAME_T pFrame);
	LPST_AUDIO_FRAME_T GetFrameFromEmptyMem();
	
	void PutFrameToPCMMem(int nChId, gm_enc_bitstream_t *pBs);
	LPST_AUDIO_FRAME_T GetFrameFromPCMMem(int nChId);
	void PutFrameToEncMem(int nChId, unsigned char* pPCMData, int nPCMLen);

	bool GetFrameFromPlayMem(unsigned char* pPCMData, int &nPCMLen);
	void PutFrameToPlayMem(unsigned char* pPCMData, int nPCMLen);
public:
	void PutFrameToDecMem(ENU_STREAMENCTYPE nType, char* pAudioData, int nLen);
	void GetFrameFromEncMem(int nChId, LPST_AUDIO_FRAME_T pFrame);

private:
	LPST_ENV_T m_pEnv;
	void* m_grab_groupfd;
	void* m_grab_bindfd[MAX_CHANNEL];
	void* m_audio_grab_object[MAX_CHANNEL];
	void* m_audio_encode_object[MAX_CHANNEL];
	pthread_t m_enc_thread_id, m_dec_thread_id, m_play_thread_id, m_cap_thread_id;
	ST_AUDIO_MEM_T m_pcm_audio_mem[MAX_CHANNEL], m_enc_audio_mem[MAX_CHANNEL], 
		m_dec_audio_mem, m_play_audio_mem, m_empty_mem;

	void *m_render_groupfd;
	void *m_render_bindfd;
	void *m_render_file_object;
	void *m_audio_render_object;

	LPST_AUDIO_PARAM m_pAudioParam;

	CAudioCodec* m_pAudioCodec;
	ENU_STREAMENCTYPE m_defAudioEncType;

	unsigned char m_leftpcm[MAX_CHANNEL][A_BITSTREAM_LEN];
	int m_leftpcm_len[MAX_CHANNEL];
};

#endif

